<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- /导航栏 -->
    <!-- 选择头像 -->
    <input @change="onChange" type="file" hidden ref="inputFile" />
    <van-cell @click="$refs.inputFile.click()" title="头像" is-link>
      <van-image class="avatar" fit="cover" round :src="user.photo" />
    </van-cell>

    <van-cell
      @click="isShowEdit = true"
      title="昵称"
      :value="user.name"
      is-link
    />

    <van-cell
      @click="isShowEditSex = true"
      title="性别"
      :value="user.gender ? '女' : '男'"
      is-link
    />
    <van-cell
      @click="isShowEditBirthday = true"
      title="生日"
      :value="user.birthday"
      is-link
    />

    <!-- 昵称弹出层 -->
    <van-popup
      v-model="isShowEdit"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <user-edit
        v-if="isShowEdit"
        v-model="user.name"
        @close="isShowEdit = false"
      />
    </van-popup>

    <!-- 性别 -->
    <van-popup v-model="isShowEditSex" position="bottom">
      <user-edit-sex
        v-if="isShowEditSex"
        v-model="user.gender"
        @close="isShowEditSex = false"
      />
    </van-popup>

    <!-- 生日 -->
    <!-- 性别 -->
    <van-popup v-model="isShowEditBirthday" position="bottom">
      <user-edit-birthday
        v-model="user.birthday"
        @close="isShowEditBirthday = false"
        v-if="isShowEditBirthday"
      />
    </van-popup>
    <!-- 头像 -->
    <van-popup v-model="isShowEditAvatar" position="bottom" :style="{ height: '100%' }">
     <user-edit-avatar @updata-avatar='user.photo=$event' v-if="isShowEditAvatar" @close='isShowEditAvatar=false' :img='avatar' />
    </van-popup>
  </div>
</template>

<script>
import { getUserinfo } from '@/api/user.js'
import UserEdit from './components/UserEdit.vue'
import UserEditSex from './components/UserEditSex.vue'
import UserEditBirthday from './components/UserEditBirthday.vue'
import UserEditAvatar from './components/UserEditAvatar.vue'
export default {
  components: { UserEdit, UserEditSex, UserEditBirthday, UserEditAvatar },
  data () {
    return {
      user: [],
      isShowEdit: false,
      isShowEditSex: false,
      isShowEditBirthday: false,
      isShowEditAvatar: false,
      avatar: ''
    }
  },

  created () {
    this.onLoadUser()
  },

  methods: {
    async onLoadUser () {
      const res = await getUserinfo()
      this.user = res
    },
    onChange (e) {
      if (e.target.value !== '') {
        this.isShowEditAvatar = true
        this.avatar = URL.createObjectURL(e.target.files[0])
        // 若选择的图片是一样的 那么value就没有变化 则不会触发onchange事件
        e.target.value = ''
      }
    }
  }
}
</script>

<style scoped lang="less">
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
  /deep/.van-nav-bar__arrow {
    color: #fff;
  }
}
</style>
